-- 对象池
ObjectPool = ObjectPool or Class("ObjectPool")

---@diagnostic disable-next-line: duplicate-set-field
function ObjectPool:ctor(creator, pop)
    self.pool = {}
    self.using = {}
    self.creator = creator
    self.pop = pop
end

function ObjectPool:get(...)
    if #self.pool == 0 then
        return self:create(...)
    else
        local obj = table.remove(self.pool, 1)
        if self.pop then
            self.pop(obj, ...)
        end
        return obj
    end
end

function ObjectPool:create(...)
    local obj = self.creator(...)
    table.insert(self.using, obj)
    return obj
end

function ObjectPool:recycle(obj)
    table.removeObj(self.using, obj)
    table.insert(self.pool, obj)
end